import {
    createPinia,defineStore,storeToRefs,mapState
} from 'pinia';
import { aesEncrypt, aesDecrypt } from 'src/utils';
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';
import env from 'src/config';

const { debuggerMode,path } = env;
export const storage = debuggerMode ?
    window.localStorage :
    {
        'getItem': key => (localStorage.getItem(key) ? aesDecrypt(localStorage.getItem(key)) : {}),
        'setItem': (key, value) => localStorage.setItem(key, aesEncrypt(value)),
        'removeItem': key => localStorage.removeItem(key)
    };
export const getCache = key => (UtilTools.typeIs(storage.getItem(key)) === 'string' ? JSON.parse(storage.getItem(key)) : storage.getItem(key));
export const setCache = (key,value) => storage.setItem(key,JSON.stringify(value));

export const pinia = createPinia();
export const keys = {
    'org':`${path}_org`,
    'sys':`${path}_sys`,
    'user':`${path}_user`
};

export {
    defineStore, piniaPluginPersistedstate,storeToRefs,mapState
};
